﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class Main_Personal_Notes : System.Web.UI.Page
{
    /*page defaults*/
    OfficeOnlineUser currentUser;

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/

    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();

        txtCurrentUser.Value = currentUser.UserName;

        if (Request.QueryString["updatenote"] == "1")
        {
            int result = UpdateNote();            
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in updating the note, please contact the system admin." + "</response>";
                    break;
            }
        }
        else if (Request.QueryString["insertnote"] == "1")
            {
                int result = InsertNote();
                switch (result)
                {
                    case 1:
                        form1.InnerHtml = "<response>" + "Success" + "</response>";
                        break;
                    default:
                        form1.InnerHtml = "<response>" + "There was an unknown problem in adding the note, please contact the system admin." + "</response>";
                        break;
                }
            }
        else if (Request.QueryString["deletenote"] == "1")
        {
            int result = DeleteNote();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in deleting the note, please contact the system admin." + "</response>";
                    break;
            }
        }
        else
        {
            BindNotesTable();

            //for (int i = 0; i < OfficeOnlineUser.UserTypes.Length; i++)
            //{
            //    if (i + 1 > currentUser.UserType)
            //    {
            //        break;
            //    }

            //    ListItem li = new ListItem(OfficeOnlineUser.UserTypes[i], (i + 1).ToString());
            //    ddlType.Items.Add(li);
            //}
        }
    }

    private void BindNotesTable()
    {
        string command = "sp_tb_user_GetUserNotes @userID='" + currentUser.UserName + "'";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        sqlCon.Open();
        SqlDataAdapter sda = new SqlDataAdapter(command, sqlCon);

        DataSet dsNotes = new DataSet();
        sda.Fill(dsNotes);

        sqlCon.Close();

        GridView1.DataSource = dsNotes;
        GridView1.DataBind();
    }

    protected void GridView1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                e.Item.Attributes.Add("onclick", "selectNoteTableElement('" + e.Item.ClientID + "')");
                e.Item.Attributes.Add("noteID", DataBinder.Eval(e.Item.DataItem, "noteID").ToString());
                e.Item.Cells[0].Style.Add("padding", "2px 0 2px 2%");
                e.Item.Cells[1].Style.Add("padding", "2px 0 2px 2%");
                //e.Item.Cells[1].ForeColor = System.Drawing.Color.Blue;
                //e.Item.Cells[1].Style.Add("text-decoration", "underline");
                //e.Item.Cells[1].Style.Add("cursor", "pointer");
                e.Item.Cells[1].Attributes.Add("title", DataBinder.Eval(e.Item.DataItem, "title").ToString());
                e.Item.Cells[2].Attributes.Add("title", DataBinder.Eval(e.Item.DataItem, "description").ToString());
                break;
        }
    }
    
    private int DeleteNote()
    {
        string noteID = Request.QueryString["noteID"];

        string command = "sp_tb_user_DeleteUserNote";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@noteID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@noteID"].Value = noteID;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }
    
    private int UpdateNote()
    {
        string noteID = Request.QueryString["noteID"];
        string title = Request.QueryString["title"];
        string description = Request.QueryString["desc"];
            
        string command = "sp_tb_user_UpdateUserNote";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@noteID", SqlDbType.Int));
        sqlCmd.Parameters.Add(new SqlParameter("@title", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@description", SqlDbType.NVarChar, 255, ""));        
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@noteID"].Value = noteID;
        sqlCmd.Parameters["@title"].Value = title;
        sqlCmd.Parameters["@description"].Value = description;
        
        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }

    private int InsertNote()
    {
        string username = Request.QueryString["username"];
        string title = Request.QueryString["title"];
        string description = Request.QueryString["desc"];

        string command = "sp_tb_user_InsertUserNote";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@title", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@description", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@userID"].Value = username;
        sqlCmd.Parameters["@title"].Value = title;
        sqlCmd.Parameters["@description"].Value = description;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }
}
